<?php
namespace app\qiandao\controller;

use think\Controller;
use think\Db;
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020 http://115cms.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: @梦雨 <50361804@qq.com>
// +----------------------------------------------------------------------
class Index extends Controller
{
    public function admin()
    {
        if (user('admin')==0) {
            $this->error('无权限', url('index/index/index'));
        }
        return $this->fetch('/admin');
    }
    public function qiandao()
    {
        if (user('admin')==0) {
            $this->error('无权限', url('index/index/index'));
        }
        if (request()->isPost()) {
            $_data['days'] = 0;
            if (!Db::name(ps('qiandaos'))->strict(false)->data($_data)->insert()) {
                $this->error('添加失败');
            } else {
                $this->success('添加成功');
            }
        }
    }
    public function tijiao()
    {
        if (!user('userid')) {
            $this->error('请先登录', url('index/index/index'));
        }
        $time = strtotime(date('Y-m-d'));
        $time1 = strtotime(date('Y-m-d', strtotime('-1 day')));
        //今天
        $yw = Db::name('qiandao')->where('uid', session('userid'))->where("stime > {$time}")->count();
        //昨天
        $ywz = Db::name('qiandao')->where('uid', session('userid'))->where("stime < {$time} and stime > {$time1}")->count();
        if ($yw > 0) {
            $this->error('今天您已签到');
        } else {
            //是否签到过
            $data['uid'] = session('userid');
            $data['ip'] = $_SERVER['REMOTE_ADDR'];
            $data['stime'] = time();
            //昨天有签到吗？
            if ($ywz > 0) {
                $days = Db::name('qiandao')->where('uid', session('userid'))->where("stime < {$time} and stime > {$time1}")->value('days');
                $dy = Db::name('qiandaos')->where("days <= {$days}")->order('id desc')->limit(1)->find();
                $data['score'] = $dy['score'];
                $score = $dy['score'];
                $data['days'] = $days + 1;
            } else {
                $score = Db::name('qiandaos')->where('days', 1)->value('score');
                $data['score'] = $score;
                $data['days'] = 1;
            }
            point($score, session('userid'), '每日签到');
            Db::name('qiandao')->insert($data);
            $this->success('签到成功');
        }
    }
}